package actions;

import bin.Mud;
import items.Item;
import items.ItemType;
import items.containers.Container;

public class GetDecider {
    private Mud mudWindow;

    public GetDecider(Mud mudWindow) {
        this.mudWindow = mudWindow;
    }

    public boolean isGet() {
        String s = mudWindow.getCommand();
        return (s.startsWith("GET" + " ") || s.equals("GET"));
    }

    public boolean isDrop() {
        String s = mudWindow.getCommand();
        return (s.startsWith("DROP" + " ") || s.equals("DROP"));
    }

    public boolean isValidGet() {
        String s = mudWindow.getCommand().replaceFirst("GET ", "");
        boolean rtn = false;
        for (Item item : mudWindow.getRoom().getRoomItems()) {
            if (item.getItemType() != ItemType.CONTAINER && (item.getTitle().equalsIgnoreCase(s) || item.getShortName().equalsIgnoreCase(s))) {
                rtn = true;
            } else if (item instanceof Container && ((Container) item).isOpen()) {
                for (Item item2 : ((Container) item).getContents()) {
                    if (item2.getTitle().equalsIgnoreCase(s) || item2.getShortName().equalsIgnoreCase(s)) {
                        rtn = true;
                    }
                }
            }
        }
        return rtn;
    }

    public boolean isValidDrop() {
        String s = mudWindow.getCommand().replaceFirst("DROP ", "");
        boolean rtn = false;
        for (Item item : mudWindow.getPlayer().getPlayerInventory(null)) {
            if (item.getTitle().equalsIgnoreCase(s) || item.getShortName().equalsIgnoreCase(s)) {
                rtn = true;
            }
        }
        return rtn;
    }

    public void pickUpItem() {
        String s = mudWindow.getCommand().replaceFirst("GET ", "");

        for (Item item : mudWindow.getRoom().getRoomItems()) {
            if (item.getTitle().equalsIgnoreCase(s) || item.getShortName().equalsIgnoreCase(s) && item.getItemType() != ItemType.CONTAINER) {
                mudWindow.getRoom().getRoomItems().remove(item);
                mudWindow.getPlayer().getPlayerInventory(null).add(item);
            } else if (item instanceof Container) {
                for (Item item2 : ((Container) item).getContents()) {
                    if (item2.getTitle().equalsIgnoreCase(s) || item2.getShortName().equalsIgnoreCase(s)) {
                        ((Container) item).getContents().remove(item2);
                        mudWindow.getPlayer().getPlayerInventory(null).add(item2);
                    }
                }
            }
        }
    }

    public String dropItem() {
        Item rtnItem = null;
        String rtn = "";
        String s = mudWindow.getCommand().replaceFirst("DROP ", "");
        for (Item item : mudWindow.getPlayer().getPlayerInventory(null)) {
            if (item.getTitle().equalsIgnoreCase(s) || item.getShortName().equalsIgnoreCase(s)) {
                rtnItem = item;
            }
        }
        if (rtnItem != null) {
            mudWindow.getRoom().getRoomItems().add(rtnItem);
            mudWindow.getPlayer().getPlayerInventory(null).remove(rtnItem);
            rtn = rtnItem.getTitle();
        }
        return rtn;
    }

    public Item itemToGet() {
        String s = mudWindow.getCommand().replaceFirst("GET ", "");
        Item itemToGet = null;

        for (Item item : mudWindow.getRoom().getRoomItems()) {
            if (item.getTitle().equalsIgnoreCase(s) || item.getShortName().equalsIgnoreCase(s) && item.getItemType() != ItemType.CONTAINER) {
                itemToGet = item;
            } else if (item instanceof Container) {
                for (Item item2 : ((Container) item).getContents()) {
                    if (item2.getTitle().equalsIgnoreCase(s) || item2.getShortName().equalsIgnoreCase(s)) {
                        itemToGet = item2;
                    }
                }
            }
        }
        return itemToGet;
    }
}